#ifndef WORKQUEUE_H__
#define WORKQUEUE_H__


#include "pt.h"
#include "ptdef.h"
#include "pt_timer.h"
enum
{
    PT_WORK_STATE_PENDING    = 0x0001,     /* Work item pending state */
//    PT_WORK_STATE_SUBMITTING = 0x0002,     /* Work item submitting state */
};
#define PT_WORK_REPEAT_FOREVER  0XFFFFFF
/**
 * work type defitions
 */
enum
{
    PT_WORK_TYPE_REPEAT     = 0x0001,   // 重复执行的工作
    PT_WORK_TYPE_SOON       = 0x0002,   // 第一次尽快执行, 即不进行延时就执行
};

/* workqueue implementation */
struct pt_workqueue
{
    pt_list_t      work_list;
    struct pt_work *work_current; /* current work */
    struct pt    *work_thread;
};

struct pt_work
{
    pt_list_t list;

    void (*work_func)(struct pt_work *work);
    void *work_data;
    uint32_t work_data_len;
    uint16_t flags;
    uint16_t type;
    int32_t do_times;

    struct timer timer;
//    struct pt_workqueue *workqueue;
};
extern struct pt_workqueue sys_workq;
int pt_work_sys_workqueue_init(void);
int32_t pt_schedule_work(struct pt_work *work);
void pt_work_bind_func(struct pt_work *work, void (*work_func)(struct pt_work *) );
void pt_work_forward_param(struct pt_work *work, void *work_data, uint32_t work_data_len);
int32_t pt_schedule_delayed_ms_work(struct pt_work *work, uint32_t time);
int32_t pt_schedule_delayed_work(struct pt_work *work, uint32_t ticks);

int32_t pt_schedule_repeat_work(struct pt_work *work, uint32_t ms, uint32_t cnt);
int32_t pt_schedule_repeat_work_soon(struct pt_work *work, uint32_t ms, uint32_t cnt);
void pt_work_cancel(struct pt_work *work);
int32_t pt_work_state(struct pt_work *work);

#define PT_SHEDULE_WORK(work, func, work_data, work_data_len)			\
	do {						                                        \
		pt_work_bind_func( work, func );                                      \
		pt_work_forward_param(work, work_data, work_data_len);          \
		pt_schedule_work( work );                                       \
	} while(0)


#define PT_SHEDULE_DELAYED_MS_WORK(work, func, time, work_data, work_data_len)			\
	do {						                                                        \
		pt_work_bind_func( work, func );                                                      \
		pt_work_forward_param(work, work_data, work_data_len);                          \
		pt_schedule_delayed_ms_work( work, time );                                      \
	} while(0)

#define PT_SCHEDULE_REPEAT_WORK(work, func, ms, cnt, work_data, work_data_len)			\
	do {						                                                        \
		pt_work_bind_func( work, func );                                                      \
		pt_work_forward_param(work, work_data, work_data_len);                          \
		pt_schedule_repeat_work( work, ms, cnt );                                      \
	} while(0)

#endif
